#
# Classifier for Postfix/Vmailer bounce messages
#
package Mail::Bounce::Exim;

use strict;

use vars qw(@ISA @patterns);

@ISA = qw(Mail::Bounce::Heuristic);

$patterns[0] = << 'EoF';
This message was created automatically by mail delivery software.

A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:

  ${RECIP-\S+}:
    ${CMD-.+}
    host ${REMOTE_MTA-\S+} [${REMOTE_IP-\S+}]:
    ${CODE-\S+} ${DIAGNOSTIC-.*}

------ This is a copy of the message, including all the headers. ------
EoF

# This pattern ws from Exim 2.05 - cien.riken.go.jp
$patterns[1] = << 'EoF';
This message was created automatically by mail delivery software.

A message that you sent could not be delivered to all of its recipients. The
following address(es) failed:

  ${RECIP-\S+}:
    ${DIAGNOSTIC2-(?s:.*)}

------ This is a copy of the message, including all the headers. ------
EoF
# note that the diagnostic may be multiline

# This pattern comes from exim 3.20 - mail.prz.tu-berlin.de
$patterns[2] = << 'EoF';
This message was created automatically by mail delivery software (Exim).

A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:

  ${RECIP-\S+}
    ${OP-.*}:
    host ${REMOTE_MTA-\S+} [${-\S+}]: ${DIAG-.*}

------ This is a copy of the message, including all the headers. ------
EoF

# This pattern is from Exim 3.12
$patterns[3] = << 'EoF';
This message was created automatically by mail delivery software.

A message that you sent could not be delivered to one or more of its
recipients. The following address(es) failed:

  ${RECIP-\S+}:
    ${DIAGNOSTIC2-(?s:.*)}

------ This is a copy of the message, including all the headers. ------
EoF

# This pattern is from Exim 3.16
$patterns[4] = << 'EoF';
This message was created automatically by mail delivery software.

A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:

  ${RECIP-\S+}:
    ${DIAGNOSTIC2-(?s:.*)}

------ This is a copy of the message, including all the headers. ------
EoF

# This pattern is from Exim 3.12 (long diagnostic)
$patterns[5] = << 'EoF';
This message was created automatically by mail delivery software.

A message that you sent could not be delivered to one or more of its
recipients. The following address(es) failed:

  ${RECIP-\S+}

The following text was generated during the delivery attempt:

${DIAGNOSTIC-(?s:.*)}

------ This is a copy of the message, including all the headers. ------
EoF

# This pattern from bham.ac.uk - Exim 3.16
# Difference to 4 is stuff on second line
$patterns[6] = << 'EoF';
This message was created automatically by mail delivery software.
${-.*}
A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:

  ${RECIP-\S+}:
    ${DIAGNOSTIC2-(?s:.*)}

------ This is a copy of the message, including all the headers. ------
EoF

# This pattern is from Exim 2.05 (long diagnostic)
$patterns[7] = << 'EoF';
This message was created automatically by mail delivery software.

A message that you sent could not be delivered to all of its recipients. The
following address(es) failed:

  ${RECIP-\S+}

The following text was generated during the delivery attempt:

${DIAGNOSTIC-(?s:.*)}

------ This is a copy of the message, including all the headers. ------
EoF

# This pattern comes from exim 3.20 - hertz.speeconnect.com
$patterns[8] = << 'EoF';
This message was created automatically by mail delivery software (Exim).

A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:

  ${RECIP-\S+}
    ${DIAG-(?s:.*)}

------ This is a copy of the message, including all the headers. ------
EoF

# This pattern is from Exim 3.02 (long diagnostic) at msk2.mail.ru
# This was also done as a nonconformant multipart/report
# AND did not have anything on the RECIP line. VERY buggy.
$patterns[9] = << 'EoF';
This message was created automatically by mail delivery software.

A message that you sent could not be delivered to all of its recipients. The
following address(es) failed:

${-\S*}${RECIP-\S*}

The following text was generated during the delivery attempt:

${DIAGNOSTIC-(?s:.*)}
EoF

sub validate {
    my $self = shift;
    my $body;
    if ($self->is_multipart()) {
	$body = $self->parts(0)->bodyhandle;
    } else {
	$body = $self->bodyhandle;
    }
    if ($self->patternmatch($body->as_string, @patterns)) {
	return 1;
    }
    return 0;
}

sub recipients {
    my $self = shift;

    my $keywords;
    my $body;
    if ($self->is_multipart()) {
	$body = $self->parts(0)->bodyhandle;
    } else {
	$body = $self->bodyhandle;
    }
    $keywords = $self->patternmatch($body->as_string, @patterns);
    if (defined($keywords)) {
	if (wantarray()) {
	    my $res = new Mail::Bounce::Address;
	    $res->type("permfail");
	    $res->action("failed");
	    if ($$keywords{CODE}) {
		if ($$keywords{CODE} eq "550") {
		    $res->status("5.1.1 (User unknown)");
		} else {
		    $res->status("5.0.0");
		}
		$res->diagnostic("Exim; " . $$keywords{CODE} . " " . $$keywords{DIAGNOSTIC})
	    } else { 
                $res->status("5.0.0 (Exim shortcut)");
	    }
	    $res->final_recipient($$keywords{RECIP});
	    return $res;
	} else {
	    return 1;
	}
    } else {
	die "Exim: Unmatched pattern in recipients()\n";
    }
}
1;




